package com.twu.biblioteca;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class BibliotecaApp {

    private static BibliotecaApp bibliotecaApp;
    private static Library library;
    private static MenuClass menu;
    private static Formatter formatter;

    public BibliotecaApp(Library library) {

        this.library =library;

        PrintStream printStream =  System.out;
        formatter = new Formatter(printStream);
        this.menu=new MenuClass(formatter);
    }

    public static void main(String []args)
    {
        bibliotecaApp=new BibliotecaApp(new Library(new Librarian(new Register())));
        while(bibliotecaApp.userLogin())
        {
            formatter.outputMessageToConsole(bibliotecaApp.getWelcomeMessage());
            bibliotecaApp.actionForUser();

        }
    }

    private boolean userLogin()
    {
        menu.getCustomerID();
        String userId= menu.getUserInput();

        menu.getCustomerPass();
        String password= menu.getUserInput();

        return login(userId, password);

    }

    private boolean login(String userId, String password) {
        boolean flag= library.loginUser(userId, password);
        if(flag)
        {
            return flag;
        }
        else
        {
            if(menu.invalidUserOption())
            {
                return userLogin();
            }
            return false;
        }
    }


    private void actionForUser()
    {
        boolean flag=true;
        while(flag)
        {
            menu.showMenu();
            String userChoiceString= menu.getUserInput();
            int userChoiceInt= menu.checkUserInput(userChoiceString);
            if(menu.menuAction(library,userChoiceInt).equalsIgnoreCase("Thank You ..."))
            {
                flag=false;
            }
        }
    }
    protected static String getWelcomeMessage() {
        return "Welcome to Biblioteca";
    }

}
